package com.xiaobaili.publisher.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SpringRabbitListener {

    /**
     * 利用@RabbitListener注解，可以监听到对应队列的消息
     * 一旦监听的队列有消息，就会回调当前方法，在方法中接收消息并消费处理消息
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "simple.queue"),
                    exchange = @Exchange(name = "xiaobaili.fanout")
            )
    })
    public void listenSimpleQueueMessage(String message) throws Exception {
        System.out.println("SpringRabbitListener listenSimpleQueueMessage 消费者接收到消息: " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "work.queue"),
                    exchange = @Exchange(name = "xiaobaili.work")
            )
    })
    public void listenWorkQueueMessageOne(String message) throws Exception {
        System.out.println("SpringRabbitListener listenWorkQueueMessageOne 消费者[1]接收到消息: " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "work.queue"),
                    exchange = @Exchange(name = "xiaobaili.work")
            )
    })
    public void listenWorkQueueMessageTwo(String message) throws Exception {
        System.out.println("SpringRabbitListener listenWorkQueueMessageTwo 消费者[2]接收到消息: " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "work.queue"),
                    exchange = @Exchange(name = "xiaobaili.work")
            )
    })
    public void listenWorkQueueMessageThree(String message) throws Exception {
        System.out.println("SpringRabbitListener listenWorkQueueMessageThree 消费者[3]接收到消息: " + message);
    }


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "xiaobaili.fanout.queue1"),
                    exchange = @Exchange(name = "xiaobaili.fanout")
            )
    })
    public void listenFanoutQueueMessageOne(String message) throws Exception {
        System.out.println("SpringRabbitListener listenFanoutQueueMessageOne 消费者[1]接收到消息: " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "xiaobaili.fanout.queue2"),
                    exchange = @Exchange(name = "xiaobaili.fanout")
            )
    })
    public void listenFanoutQueueMessageTwo(String message) throws Exception {
        System.out.println("SpringRabbitListener listenFanoutQueueMessageTwo 消费者[2]接收到消息: " + message);
    }


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "xiaobaili.direct.queue1"),
                    exchange = @Exchange(name = "xiaobaili.direct", type = ExchangeTypes.DIRECT),
                    key = {"queue0", "queue1"}
            )
    })
    public void listenDirectQueueMessageThree(String message) throws Exception {
        System.out.println("Direct消费者[1]接收到消息: " + message);
    }


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "xiaobaili.direct.queue2"),
                    exchange = @Exchange(name = "xiaobaili.direct", type = ExchangeTypes.DIRECT),
                    key = {"queue2", "queue3"}
            )
    })
    public void listenDirectQueueMessageTwo(String message) throws Exception {
        System.out.println("Direct消费者[2]接收到消息: " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "xiaobaili.topic.queue1"),
                    exchange = @Exchange(name = "xiaobaili.direct", type = ExchangeTypes.TOPIC),
                    key = {"china.#"}
            )
    })
    public void listenTopicQueueMessageOne(String message) throws Exception {
        System.out.println("Topic消费者[1]接收到消息: " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "xiaobaili.topic.queue2"),
                    exchange = @Exchange(name = "xiaobaili.direct", type = ExchangeTypes.TOPIC),
                    key = {"#.news"}
            )
    })
    public void listenTopicQueueMessageTwo(String message) throws Exception {
        System.out.println("Topic消费者[2]接收到消息: " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "xiaobaili.topic.queue3"),
                    exchange = @Exchange(name = "xiaobaili.direct", type = ExchangeTypes.TOPIC),
                    key = {"china.*"}
            )
    })
    public void listenTopicQueueMessageThree(String message) throws Exception {
        System.out.println("Topic消费者[3]接收到消息: " + message);
    }


}
